package com.ujiuye.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.ujiuye.config.MessageConstant;
import com.ujiuye.config.PageResult;
import com.ujiuye.config.QueryPageBean;
import com.ujiuye.config.Result;
import com.ujiuye.pojo.Checkgroup;
import com.ujiuye.service.ICheckgroupService;
import com.ujiuye.service.ICheckitemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 检查组 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {


    @Reference
    private ICheckgroupService checkgroupService;
    @PostMapping("list")
    public PageResult list(@RequestBody QueryPageBean queryPageBean){
        try {
            return checkgroupService.PageList(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("save")
    public Result save(Integer [] checkitemIds, @RequestBody Checkgroup checkgroup){
        try {
            checkgroupService.save(checkitemIds,checkgroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") int id){
        try {
            checkgroupService.removeById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

    }
     @GetMapping("/{id}")
    public Result getById(@PathVariable("id")int id){
         try {
             Result result = checkgroupService.getById(id);
             return result;
         } catch (Exception e) {
             e.printStackTrace();
             return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
         }
     }

     @PutMapping("update")
     public Result update(Integer[] checkitemIds,@RequestBody Checkgroup checkgroup){
         try {
             checkgroupService.update(checkitemIds,checkgroup);
             return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
         } catch (Exception e) {
             e.printStackTrace();
             return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
         }
     }

}

